package com.etop.ondoc.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.etop.ondoc.beans.*;
import com.etop.ondoc.util.MD5;
import com.etop.ondoc.util.Pager;
import com.github.cuter44.nyafx.dao.DaoBase;

public class BUserDao extends DaoBase<User> {
    /*	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");*/

    public BUserDao() {
        super();
    }

    // SINGLETON

    private static class Singleton {

        public static BUserDao instance = new BUserDao();
    }

    public static BUserDao getInstance() {
        return (Singleton.instance);
    }

    @Override
    protected Class classOfT() {
        return User.class;
    }

    public boolean Checklogin(String lun, String lpw) {
        //boolean flag = true; 下面返回U使用的
        boolean flag = false;
        if (lpw != null) {
            lpw = new MD5().getkeyBeanofStr(lpw);
        }
		//这个是语句查找但是注册登录得分开写
		/*User u =(User) this.createQuery("from User where username = "+"'"+lun+"'" +"and password = " +"'"+lpw+"'").uniqueResult();
         if(u==null){
         flag = false;
         }*/
        //注册登录合在一起，但是返回结果：（注册-lpw为null）返回false可以注册，（登录）返回true可以登录
        DetachedCriteria dc = DetachedCriteria.forClass(User.class);
        if (lun != null) {
            dc.add(Restrictions.eq("username", lun));
        }
        if (lpw != null) {
            dc.add(Restrictions.eq("password", lpw));
        }
        List<User> l = (List<User>) this.search(dc);
        if (l.size() > 0) {
            flag = true;
        }
        this.close();
        return flag;
    }

    public User CheckforU(String lun, String lpw) {
        User u = null;
        if (lpw != null) {
            lpw = new MD5().getkeyBeanofStr(lpw);
        }
        u = (User) this.createQuery("from User where username = " + "'" + lun + "'" + "and password = " + "'" + lpw + "'").uniqueResult();
        this.close();
        return u;
    }

    /**
     * 根据username获取 user
     */
    public User loadByUsername(String username) {
        User user = null;
        this.begin();
        user = (User) this.createQuery("select us from User us where username like ?")
                .setParameter(0, username).uniqueResult();
        this.close();
        return user;
    }

    public List<User> loadByUsernameList(String username) {
        List<User> users = null;
        this.begin();
        users = this.createQuery("from User where username=:username")
                .setString("username", username)
                .list();
        this.close();
        return users;
    }

    /**
     * 根据username获取 user
     */
    public User forName(String name) {
        DetachedCriteria dc = DetachedCriteria.forClass(User.class).add(
                Restrictions.eq("username", name));

        User u = null;
        try {
            u = this.get(dc);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.close();
        }
        return u;
    }

    public void add(User user) {
        if (user != null) {
            try {
                this.begin();
                this.save(user);
                this.commit();
            } catch (Exception e) {
                throw e;
            } finally {
                this.close();
            }

        }
    }

    public Pager<User> pager(int offset, int pagesize) {

        Pager<User> pages = null;
        try {
            pages = new Pager<>();
            DetachedCriteria dc = DetachedCriteria.forClass(User.class);
            this.begin();

            List<User> l = this.search(dc, offset, pagesize);
            long totalRecord = (long) this.createQuery("select count(*) from User").uniqueResult();
            pages.setDatas(l);
            pages.setPageOffset(offset);
            pages.setPageSize(pagesize);
            pages.setTotalRecord((int) totalRecord);

            this.commit();
        } catch (Exception e) {
            throw e;
        } finally {
            this.close();
        }
        return pages;
    }

}
